package com.sean.learnmybatis.controller;

import com.sean.learnmybatis.entity.Order;
import com.sean.learnmybatis.service.IOrderService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private IOrderService orderService;

    private final List<Order> list = new ArrayList<>();
    private final List<Order> updateList = new ArrayList<>();

    @PostConstruct
    public void init(){
        for (int i = 1; i <= 10000; i++){
            Order order = new Order();
            order.setId((long)i);
            order.setProductId(1L);
            order.setStatus(1);
            order.setTotalAmount(1.0);
            list.add(order);
        }

        for (int i = 1; i <= 10000; i++){
            Order order = new Order();
            order.setId((long)i);
            order.setProductId(4L);
            order.setStatus(2);
            order.setTotalAmount(2.0);
            updateList.add(order);
        }
    }

    @GetMapping("/mybatis-plus-batch-insert")
    public String mybatisPlusBatchInsert(){
        long stime = System.currentTimeMillis();
        orderService.saveBatch(list);
        long etime = System.currentTimeMillis();
        return "mybatis plus 批量插入 1w 条数据的时间: " + (etime - stime) / 1000.0 + "秒";
    }

    @GetMapping("/mybatis-batch-insert")
    public String mybatisBatchInsert(){
        long stime = System.currentTimeMillis();
        orderService.mySaveBatch(list);
        long etime = System.currentTimeMillis();
        return "mybatis 批量插入 1w 条数据的时间: " + (etime - stime) / 1000.0 + "秒";
    }

    @GetMapping("/mybatis-plus-batch-update")
    public String mybatisPlusBatchUpdate(){
        long stime = System.currentTimeMillis();
        orderService.updateBatchById(updateList);
        long etime = System.currentTimeMillis();
        return "mybatis plus 批量更新 1w 条数据的时间: " + (etime - stime) / 1000.0 + "秒";
    }

    @GetMapping("/mybatis-batch-update")
    public String mybatisBatchUpdate(){
        long stime = System.currentTimeMillis();
        orderService.myUpdateBatchById(updateList);
        long etime = System.currentTimeMillis();
        return "mybatis 批量更新 1w 条数据的时间: " + (etime - stime) / 1000.0 + "秒";
    }
}
